<?php
/**
 * Created by PhpStorm.
 * User: 汪利东
 * Date: 2016/3/21
 * Time: 14:43
 */

namespace Admin\Controller;


class UserController extends BaseController
{
    public function index()
    {
        $map = [];
        $user_id = I('get.user_id');
        if (!empty($user_id)) {
            $map['user_id'] = $user_id;
        }
        $keywords = I('get.keywords');
        if (!empty($keywords)) {
            $map['nickname'] = ['like', "%$keywords%"];
            $map['_logic'] = 'OR';
        }

        $count = D('user')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出

        $data = D('user')
            ->relation(true)
            ->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('user_id')
            ->select();
        $this->data = $data;

        $this->display();
    }

    public function address()
    {
        $user_id = I('user_id');
        $address = M('address')->where(['user_id' => $user_id])->find();
        $this->assign('data',$address);
        $this->assign('user_id',$user_id);
        $this->display();
    }

    public function editAddress()
    {
        $request = I('post.');
        $address = M('address')->where(['user_id'=>$request['user_id']])->find();
        if ($address) {
            //编辑
            $r = M('address')->where(['user_id'=>$request['user_id']])->save($request);
            if (!empty($r)) {
                $this->success('修改成功');
            } else {
                $this->success('修改失败');
            }
        } else {
            // 新增
            $request['is_default'] = 1;
            $r = M('address')->add($request);
            if (!empty($r)) {
                $this->success('新增成功', U('index'));
            } else {
                $this->success('新增失败');
            }
        }
    }

    function export() {
        $data = D('user')->alias('a')
            ->join('left join card b on a.user_id=b.user_id')
            ->field("b.card_id,a.user_id,a.nickname,b.father_phone,b.child_name")
            ->order('user_id')->select();
        $file = excel_export($data);
        ouput_Browser($file);
    }

}